<script setup lang="ts">

import { AnswerVO } from '@/api/exam/answer/types';
import { h } from 'vue';
import { CloseOutlined } from '@ant-design/icons-vue';
import { QuestionType } from '@/api/exam/template/types';
import { getChooseLabel } from '@/utils/tagColor';
import { updateAnswerScore } from '@/api/exam/answer';

interface Props {
  answerList: any | Array<AnswerVO>;
  drawer: any;
}

const props = withDefaults(defineProps<Props>(), {
  answerList: [],
  drawer: {
    visible: false,
    title: ''
  }
});
const emit = defineEmits(['manual-close']);
const answerList = ref<AnswerVO[]>();
const drawer = ref<DrawerOption>({
  visible: false,
  title: ''
});
watch(() => props.drawer.visible, () => {
  answerList.value = props.answerList;
  handleMenuClick(0);
  drawer.value = props.drawer;
});

const currentMenuIndex = ref(0);
const currentAnswerInfo = ref<AnswerVO>();
const selectedKeys = ref([0]);
const currentLoading = ref(false);

const handleMenuClick = (index: number) => {
  currentMenuIndex.value = index;
  currentAnswerInfo.value = answerList.value[index];
};
const timeDifference = computed(() => {
  const endTime = currentAnswerInfo.value.metaInfo.answerInfo.endTime;
  const startTime =currentAnswerInfo.value.metaInfo.answerInfo.startTime;
  return formatTimeDifference((Date.parse(endTime) - Date.parse(startTime))/1000);
});
function formatTimeDifference(seconds) {
  // 确保输入是数字类型
  seconds = parseFloat(seconds);

  // 处理小于0的情况
  if (isNaN(seconds) || seconds < 0) {
    return "0秒";
  }

  // 计算分钟和剩余秒数
  const minutes = Math.floor(seconds / 60);
  const remainingSeconds = Math.floor(seconds % 60);

  // 确保秒数始终显示为两位数字
  const formattedSeconds = remainingSeconds < 10 ?
    `0${remainingSeconds}` :
    remainingSeconds;

  return `${minutes} 分 ${formattedSeconds} 秒`;
}
// 点击True图标操作
const checkTrueIcon = (id: any) => {
  currentAnswerInfo.value.examInfo['questionScore'][id] =
    currentAnswerInfo.value.survey.children.find((item: any) => item.id === id).attribute.examScore;
  handleSubmitCurrentScore();
};
// 点击False图标操作
const checkFalseIcon = (id: any) => {
  currentAnswerInfo.value.examInfo['questionScore'][id] = 0;
  handleSubmitCurrentScore();
};
/*数字输入框值发生变化时提交*/
const handleSubmitCurrentScore = async () => {
  const res = await updateAnswerScore(currentAnswerInfo.value);
  currentAnswerInfo.value = res.data;
};
// 上一份
const prevAnswer = () => {
  currentMenuIndex.value--;
  selectedKeys.value = [currentMenuIndex.value];
  handleMenuClick(currentMenuIndex.value);
};
// 下一份
const nextAnswer = () => {
  currentMenuIndex.value++;
  selectedKeys.value = [currentMenuIndex.value];
  handleMenuClick(currentMenuIndex.value);
};

const handleClose = () => {
  emit('manual-close');
};


</script>

<template>
  <el-drawer v-model="drawer.visible" size="100%" :show-close="false"
             :with-header="false" class="full-drawer">
    <el-header class="flex manual-ranking-header items-center gap-2">
      <el-tooltip content="关闭">
        <a-button type="text" @click="handleClose()" :icon="h(CloseOutlined)" />
      </el-tooltip>
      <div class="pe-view">{{ drawer.title }}</div>
    </el-header>
    <el-container>
      <el-main>
        <div class="w-80% ma">
          <a-row :gutter="20" style="max-height: 85vh">
            <a-col :span="5">
              <a-flex vertical gap="small">
                <a-card>
                  <template #title>
                    <div class="flex justify-between items-center">
                      <el-text tag="b">评分名单</el-text>
                      <el-text>{{ currentMenuIndex + 1 }} / {{ answerList.length }}</el-text>
                    </div>
                  </template>
                  <a-menu v-model:selected-keys="selectedKeys"
                          class="" style="overflow-y: auto;height: 62vh">
                    <a-menu-item v-for="(item, index) in answerList" :key="index"
                                 :index="index" @click="handleMenuClick(index)">
                      答卷 {{ index + 1 }}
                    </a-menu-item>
                  </a-menu>
                </a-card>
                <a-card>
                  <a-flex justify="space-between" align="center">
                    <span>只显示未评分</span>
                    <a-switch />
                  </a-flex>
                </a-card>
                <a-card>
                  答题时长:
                  {{ timeDifference }}
                </a-card>
              </a-flex>
            </a-col>
            <a-col :span="14">
              <a-card v-loading="currentLoading">
                <template #title>
                  <el-text class="card-title">答题详细</el-text>
                </template>
                <div class="h-77vh" ref="containerRef">
                  <el-scrollbar>
                    <div :id="question.id" class="pl-6 pr-6 "
                         v-for="(question, index) in currentAnswerInfo.survey.children"
                    >
                      <div class="p-4 mt-2 vertical">
                        <a-flex justify="space-between" class="mb-2">
                          <el-text size="small">第 {{ index + 1 }} 题</el-text>
                          <a-flex gap="small" align="center">
                            <el-tag v-if="currentAnswerInfo.examInfo['questionScore'][question.id]!==0" type="success">
                              得分: {{ currentAnswerInfo.examInfo['questionScore'][question.id] }}
                            </el-tag>
                            <svg-icon
                              v-if="currentAnswerInfo.examInfo['questionScore'][question.id]=== question.attribute.examScore"
                              icon-class="true" color="#00bf6f" />
                            <svg-icon v-else icon-class="false" color="#ff6d56" />
                          </a-flex>

                        </a-flex>
                        <div class="form-item-title">
                          <el-text size="large"> {{ question.title }}</el-text>
                        </div>
                        <div class="flex">
                          <div style="flex: 1 1 0%;">
                            <div v-if="question.description!==null&&question.description!==undefined"
                                 class="form-item-description">
                              <p class=" question-title ml-10">{{ question.description }}</p>
                            </div>
                            <a-radio-group
                              v-if="question.type === QuestionType.Radio ||question.type === QuestionType.TrueFalse"
                              v-model:value="currentAnswerInfo.tempAnswer[question.id]">
                              <a-row :gutter="[16,8]">
                                <a-col :span="24" v-for="(item,i) in question.children"
                                       :class="{'right-answer': item.attribute.examCorrectAnswer===currentAnswerInfo.tempAnswer[question.id]&&currentAnswerInfo.tempAnswer[question.id]===item.id,
                                       'wrong-answer': item.attribute.examCorrectAnswer!==currentAnswerInfo.tempAnswer[question.id]&&currentAnswerInfo.tempAnswer[question.id]===item.id}"
                                       class="default-radio-answer">
                                  <a-radio size="large" :value="item.id" @click.prevent>
                                    <span class="ml-2">{{ getChooseLabel(i) + item.title }}</span>
                                  </a-radio>
                                </a-col>
                              </a-row>
                            </a-radio-group>
                            <a-checkbox-group v-if="question.type ===QuestionType.Checkbox"
                                              v-model:value="currentAnswerInfo.tempAnswer[question.id]">
                              <a-row :gutter="[16,8]">
                                <a-col v-for="(item, idx) in question.children" :key="idx" :span="24"
                                       :class="{'right-answer':  currentAnswerInfo.tempAnswer[question.id].includes(item.attribute.examCorrectAnswer) && currentAnswerInfo.tempAnswer[question.id].includes(item.id),
                           'wrong-answer': !currentAnswerInfo.tempAnswer[question.id].includes(item.attribute.examCorrectAnswer) && currentAnswerInfo.tempAnswer[question.id].includes(item.id),}"
                                       class="default-checkbox-answer">
                                  <a-checkbox :value="item.id" @click.prevent>
                                    {{ item.title }}
                                  </a-checkbox>
                                </a-col>
                              </a-row>
                            </a-checkbox-group>
                            <div
                              v-if="question.type === QuestionType.MultipleBlank || question.type === QuestionType.FillBlank">
                              <a-row :gutter="[16,8]">
                                <a-col v-for="(option, idx) in question.children" :key="idx" :span="24">
                                  <a-input v-model:value="currentAnswerInfo.tempAnswer[question.id][option.id]"
                                           readonly />
                                </a-col>
                              </a-row>
                            </div>
                            <div v-if="question.type ===QuestionType.Textarea">
                              <a-row :gutter="[16,8]">
                                <a-col v-for="(option, idx) in question.children" :key="idx" :span="24">
                                  <a-textarea v-model:value="currentAnswerInfo.tempAnswer[question.id][option.id]"
                                              :autosize="{ minRows: 2, maxRows: 6 }" resize="none" readonly />
                                </a-col>
                              </a-row>
                            </div>
                          </div>
                          <div class="manual-score">
                            <div class="choose-right-wrong">
                              <el-text>是否正确</el-text>
                              <svg-icon icon-class="true"
                                        :color="currentAnswerInfo.examInfo['questionScore'][question.id]=== question.attribute.examScore?'#2cce35':'rgba(170,178,189,0.6)'"
                                        class="choose-svg cursor-pointer"
                                        @click="checkTrueIcon(question.id)" />
                              <svg-icon icon-class="false"
                                        :color="currentAnswerInfo.examInfo['questionScore'][question.id]< question.attribute.examScore?'rgb(223, 66, 66)':'rgba(170,178,189,0.6)'"
                                        class="choose-svg cursor-pointer"
                                        @click="checkFalseIcon(question.id)" />
                            </div>
                            <div class="choose-right-wrong">
                              <div class="w-20 ml-5">
                                <el-text>得分</el-text>
                              </div>
                              <a-input-number v-if="currentAnswerInfo.examInfo" size="small"
                                              v-model:value="currentAnswerInfo.examInfo['questionScore'][question.id]"
                                              :min="0" :value-on-clear="0" :max="question.attribute.examScore"
                                              @change="handleSubmitCurrentScore()" />
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </el-scrollbar>
                </div>
              </a-card>
            </a-col>
            <a-col :span="5" v-if="currentAnswerInfo">
              <a-flex vertical gap="small">
                <a-card>
                  <a-flex gap="middle" align="center">
                    <span>当前得分 </span>
                    <el-text tag="b" size="large"> {{ currentAnswerInfo.examScore }}</el-text>
                  </a-flex>
                </a-card>
                <a-card>
                  <template #title>
                    <el-text class="card-title">答题卡</el-text>
                  </template>
                  <div style="height: 60vh;overflow-y: auto">
                    <a :href="'#'+key" v-for="(value, key,index) in currentAnswerInfo.examInfo['questionScore']"
                       class="answer-sheet-item item-danger"
                       :class="{ 'item-success': currentAnswerInfo.examInfo['questionScore'][key]===currentAnswerInfo.survey.children[index].attribute.examScore }">
                      {{ index + 1 }}
                    </a>
                  </div>
                </a-card>
                <a-card>
                  <a-flex justify="space-between">
                    <a-button :disabled="currentMenuIndex === 0" @click="prevAnswer()">
                      <svg-icon icon-class="prev" class="mr-3" />
                      上一份
                    </a-button>
                    <a-button @click="nextAnswer()"
                              :disabled="currentMenuIndex === answerList.length - 1">
                      下一份
                      <svg-icon icon-class="next" class="ml-3" />
                    </a-button>
                  </a-flex>
                </a-card>
              </a-flex>
            </a-col>
          </a-row>
        </div>

      </el-main>
    </el-container>
  </el-drawer>
</template>

<style scoped lang="scss">
.answer-sheet-item {
  position: relative;
  float: left;
  box-sizing: border-box;
  width: 32px;
  height: 32px;
  margin: 4.5px;
  color: #484848;
  font-size: 12px;
  line-height: 32px;
  white-space: nowrap;
  text-align: center;
  background: #fff;
  border: 1px solid #d8d8d8;
  border-radius: 50%;
  cursor: pointer;
}

.item-danger {
  color: var(--el-color-danger);
  background: var(--el-color-danger-light-9);
  border-color: var(--el-color-danger-light-5);
}

.item-success {
  color: var(--el-color-success);
  background: var(--el-color-success-light-9);
  border-color: var(--el-color-success-light-5);
}

.manual-score {
  width: 144px;
  padding-bottom: 6px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;

  .choose-right-wrong {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-top: 12px;
    gap: 10px;

    .choose-svg {
      font-size: 24px;
    }
  }
}
</style>
